const { Controller } = require('varal');
const { sequelize } = require('../../models');
const { QueryTypes, Op } = require('sequelize');

class WithdrawalController extends Controller {
	async index(app) {
		const { id: user_id } = app.auth;

		const serv = this.app.service('WithdrawalService');
		const { data } = await serv.get({
			user_id
		}, 1, 1000);

		this.app.json({
			err_code: 0,
			data
		})
	}

	async apply(app) {
		const { id: user_id } = app.auth;
		const params = app.fields;

		const serv = this.app.service("WithdrawalService");

		//获取提现后的余额
		const { balance = 0 } = await app.service('UserService').info({ id: user_id });
		params.balance = balance - params.amount;

		await serv.save({
			...params,
			user_id
		});

		this.app.json({
			err_code: 0
		})
	}
}


exports = module.exports = WithdrawalController;
